import borax
from borax.calendars import LunarDate
from CommonVarRule import CommonVarRule

class CalcCore:
    # 常量定义（模拟C#中的CommonVarRule）
    CONST_TIANGAN = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
    CONST_DIZHI = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
    DiZhi_No_MAP = {
        "子": 1, "丑": 2, "寅": 3, "卯": 4, "辰": 5, "巳": 6,
        "午": 7, "未": 8, "申": 9, "酉": 10, "戌": 11, "亥": 12
    }

    # 枚举定义
    class YinYangEnum:
        阳男 = 0
        阴女 = 1

    @staticmethod
    def DiZhiMoveNext(dizhi: str) -> str:
        """根据地支获取下一个地支"""
        idx = CalcCore.CONST_DIZHI.index(dizhi)
        return CalcCore.CONST_DIZHI[(idx + 1) % 12]

    @staticmethod
    def DiZhiMoveBefore(dizhi: str) -> str:
        """根据地支获取上一个地支"""
        idx = CalcCore.CONST_DIZHI.index(dizhi)
        return CalcCore.CONST_DIZHI[(idx - 1) % 12]

    @staticmethod
    def GetYanShouByTianGan(tianGan: str) -> str:
        """根据天干获取寅首"""
        yDict = {
            "丙": "甲己",
            "戊": "乙庚",
            "庚": "丙辛",
            "壬": "丁壬",
            "甲": "戊癸"
        }
        yearTianGan = tianGan[0]
        for key, value in yDict.items():
            if yearTianGan in value:
                return key
        return ""

    @staticmethod
    def GetTwelveGongTianGan(yinShouTianGan: str) -> list:
        tianGanIdx = 0
        for i, gan in enumerate(CalcCore.CONST_TIANGAN):
            if gan == yinShouTianGan:
                tianGanIdx = i
                break

        tmpGongWeiTianGan = []
        for i in range(2, 14):
            if i < 12:
                zizhi = CalcCore.CONST_DIZHI[i]
            else:
                zizhi = CalcCore.CONST_DIZHI[i % 12]

            if tianGanIdx >= 10:
                tianGanIdx = 0

            tianGan = CalcCore.CONST_TIANGAN[tianGanIdx]
            tmpGongWeiTianGan.append(tianGan + zizhi)
            tianGanIdx += 1

        return tmpGongWeiTianGan

    @staticmethod
    def GetMingGongAndShenGong(monthNum: int, hourNum: int) -> list:
        shenggongIdx = monthNum + hourNum
        shenGongDiZhi = CalcCore.CONST_DIZHI[shenggongIdx % 12]

        idx = 14 + (monthNum - hourNum)
        ming = CalcCore.CONST_DIZHI[idx % 12]

        return [ming, shenGongDiZhi]

    @staticmethod
    def GetMingZhuXing(mingGongDizhi: str) -> str:
        dDoct = {
            "子": "贪狼",
            "丑亥": "巨门",
            "寅戌": "禄存",
            "卯酉": "文曲",
            "巳未": "武曲",
            "辰申": "廉贞",
            "午": "破军"
        }
        for key, value in dDoct.items():
            if mingGongDizhi in key:
                return value
        return ""

    @staticmethod
    def GetShenZhuXing(yearDiZhi: str) -> str:
        dDoct = {
            "子": "铃星",
            "午": "火星",
            "丑未": "天相",
            "寅申": "天梁",
            "卯酉": "天同",
            "巳亥": "天机",
            "辰戌": "文昌"
        }
        for key, value in dDoct.items():
            if yearDiZhi in key:
                return value
        return ""

    @staticmethod
    def GetWuXingJu(mingGan: str, mingZhi: str) -> str:
        dict_rules = {
            "甲乙子丑午未": "金四局",
            "甲乙寅卯申酉": "水二局",
            "甲乙辰巳戌亥": "火六局",
            "丙丁子丑午未": "水二局",
            "丙丁寅卯申酉": "火六局",
            "丙丁辰巳戌亥": "土五局",
            "戊己子丑午未": "火六局",
            "戊己寅卯申酉": "土五局",
            "戊己辰巳戌亥": "木三局",
            "庚辛子丑午未": "土五局",
            "庚辛寅卯申酉": "木三局",
            "庚辛辰巳戌亥": "金四局",
            "壬癸子丑午未": "木三局",
            "壬癸寅卯申酉": "金四局",
            "壬癸辰巳戌亥": "水二局"
        }
        for key, value in dict_rules.items():
            if mingGan in key and mingZhi in key:
                return value
        return ""

    @staticmethod
    def GetZiWeiXingAtGongWei(nongliDay: str, wuXingJu: str) -> str:
        wuxingJuMap = {
            "水二局": 0,
            "木三局": 1,
            "金四局": 2,
            "土五局": 3,
            "火六局": 4
        }

        dayMap = {
            "初一": 0, "初二": 1, "初三": 2, "初四": 3, "初五": 4,
            "初六": 5, "初七": 6, "初八": 7, "初九": 8, "初十": 9,
            "一": 0, "二": 1, "三": 2, "四": 3, "五": 4,
            "六": 5, "七": 6, "八": 7, "九": 8, "十": 9,
            "十一": 10, "十二": 11, "十三": 12, "十四": 13, "十五": 14,
            "十六": 15, "十七": 16, "十八": 17, "十九": 18,
            "二十": 19, "二十一": 20, "二十二": 21, "二十三": 22, "二十四": 23,
            "二十五": 24, "二十六": 25, "二十七": 26, "二十八": 27, "二十九": 28,
            "廿": 19, "廿一": 20, "廿二": 21, "廿三": 22, "廿四": 23,
            "廿五": 24, "廿六": 25, "廿七": 26, "廿八": 27, "廿九": 28,
            "三十": 29, "卅": 29
        }

        mTable = [
            ["丑", "辰", "亥", "午", "酉"],
            ["寅", "丑", "辰", "亥", "午"],
            ["寅", "寅", "丑", "辰", "亥"],
            ["卯", "巳", "寅", "丑", "辰"],
            ["卯", "寅", "子", "寅", "丑"],
            ["辰", "卯", "巳", "未", "寅"],
            ["辰", "午", "寅", "子", "戌"],
            ["巳", "卯", "卯", "巳", "未"],
            ["巳", "辰", "丑", "寅", "子"],
            ["午", "未", "午", "卯", "巳"],
            ["午", "辰", "卯", "申", "寅"],
            ["未", "巳", "辰", "丑", "卯"],
            ["未", "申", "寅", "午", "亥"],
            ["申", "巳", "未", "卯", "申"],
            ["申", "午", "辰", "辰", "丑"],
            ["酉", "酉", "巳", "酉", "午"],
            ["酉", "午", "卯", "寅", "卯"],
            ["戌", "未", "申", "未", "辰"],
            ["戌", "戌", "巳", "辰", "子"],
            ["亥", "未", "午", "巳", "酉"],
            ["亥", "申", "辰", "戌", "寅"],
            ["子", "亥", "酉", "卯", "未"],
            ["子", "申", "午", "申", "辰"],
            ["丑", "酉", "未", "巳", "巳"],
            ["丑", "子", "巳", "午", "丑"],
            ["寅", "酉", "戌", "亥", "戌"],
            ["寅", "戌", "未", "辰", "卯"],
            ["卯", "丑", "申", "酉", "申"],
            ["卯", "戌", "午", "午", "巳"],
            ["辰", "亥", "亥", "未", "午"]
        ]

        rowIdx = dayMap.get(nongliDay, 0)
        clnIdx = wuxingJuMap.get(wuXingJu, 0)
        return mTable[rowIdx][clnIdx]

    @staticmethod
    def Get_ShiSiZhuXingLayout(ziWeiAtDiZhi: str) -> dict:
        diZhiGongWei = {dz: [] for dz in CalcCore.CONST_DIZHI}

        tianFuPostion = {
            "子": "辰", "丑": "卯", "寅": "寅", "卯": "丑", "辰": "子",
            "巳": "亥", "午": "戌", "未": "酉", "申": "申", "酉": "未",
            "戌": "午", "亥": "巳"
        }

        ziWeiDiZhiIdx = CalcCore.DiZhi_No_MAP[ziWeiAtDiZhi] - 1
        tianFuDiZhiIdx = CalcCore.DiZhi_No_MAP[tianFuPostion[ziWeiAtDiZhi]] - 1

        ziWeiSeriesLayout = ["紫微", "", "", "", "廉贞", "", "", "天同", "武曲", "太阳", "", "天机"]
        tianFuSeriesLayout = ["天府", "太阴", "贪狼", "巨门", "天相", "天梁", "七杀", "", "", "", "破军", ""]

        # 紫微星系
        for item in ziWeiSeriesLayout:
            if item:
                diZhiGongWei[CalcCore.CONST_DIZHI[ziWeiDiZhiIdx % 12]].append(item)
            ziWeiDiZhiIdx = (ziWeiDiZhiIdx + 1) % 12

        # 天府星系
        for item in tianFuSeriesLayout:
            if item:
                diZhiGongWei[CalcCore.CONST_DIZHI[tianFuDiZhiIdx % 12]].append(item)
            tianFuDiZhiIdx = (tianFuDiZhiIdx + 1) % 12

        return diZhiGongWei

    @staticmethod
    def Get_FuBi_XingAtDiZhi(monthNo: int) -> list:
        zuoFuXIngDiZhi = CalcCore.CONST_DIZHI[(monthNo + 3) % 12]
        youbiFuXingDiZhi = CalcCore.CONST_DIZHI[(11 - monthNo) % 12]
        return [zuoFuXIngDiZhi, youbiFuXingDiZhi]

    @staticmethod
    def Get_WenChangWenQu_AtDiZhi(hourDiZhi: str) -> list:
        hourNo = CalcCore.DiZhi_No_MAP[hourDiZhi] - 1
        wenQuDiZhi = CalcCore.CONST_DIZHI[(hourNo + 3) % 12]
        wenChangDiZhi = CalcCore.CONST_DIZHI[(11 - hourNo) % 12]
        return [wenQuDiZhi, wenChangDiZhi]

    # 其他方法类似转换，这里只展示部分关键方法

    @staticmethod
    def Get_ShiGanSiHuaXing(tianGan: str) -> list:
        rDict = {
            "甲": ["廉贞", "破军", "武曲", "太阳"],
            "乙": ["天机", "天梁", "紫微", "太阴"],
            "丙": ["天同", "天机", "文昌", "廉贞"],
            "丁": ["太阴", "天同", "天机", "巨门"],
            "戊": ["贪狼", "太阴", "右弼", "天机"],
            "己": ["武曲", "贪狼", "天梁", "文曲"],
            "庚": ["太阳", "武曲", "太阴", "天同"],
            "辛": ["巨门", "太阳", "文曲", "文昌"],
            "壬": ["天梁", "紫微", "左辅", "武曲"],
            "癸": ["破军", "巨门", "太阴", "贪狼"]
        }
        return rDict.get(tianGan, [])

    @staticmethod
    def GetDaXian(mingZao, shierGong):
        if not mingZao.WuXingJu or not shierGong:
            return

        wuXingJu = {
            "金四局": 4,
            "木三局": 3,
            "土五局": 5,
            "火六局": 6,
            "水二局": 2
        }

        startNum = wuXingJu.get(mingZao.WuXingJu, 0)
        mingGong = next((g for g in shierGong if g.GongName == "命宫"), None)
        if not mingGong:
            return

        randDict = {}
        if mingZao.SexNameString in ["阳男", "阴女"]:
            curDizhi = mingGong.GongDiZhi
            randDict[curDizhi] = f"{startNum}-{startNum + 9}"
            for i in range(1, 12):
                curDizhi = CalcCore.DiZhiMoveNext(curDizhi)
                randDict[curDizhi] = f"{startNum + i * 10}-{startNum + i * 10 + 9}"
        else:
            curDizhi = mingGong.GongDiZhi
            randDict[curDizhi] = f"{startNum}-{startNum + 9}"
            for i in range(1, 12):
                curDizhi = CalcCore.DiZhiMoveNext(curDizhi)
                end_val = (120 + startNum) - i * 10 + 9
                randDict[curDizhi] = f"{(120 + startNum) - i * 10}-{end_val}"

        for gong in shierGong:
            if gong.GongDiZhi in randDict:
                gong.TimeRangeType = "大限"
                gong.TimeRangeName = randDict[gong.GongDiZhi]

    # 其他辅助方法...


@staticmethod
def Get_DiJieDiKong_AtDiZhi(hourDiZhi: str) -> list:
    hourNo = CalcCore.DiZhi_No_MAP[hourDiZhi]
    # 1. 地劫
    # 亥宫起    1 <=>11   2   12  3 13
    if hourNo == 1:
        diJieDiZhi = "亥"
    else:
        diJieDiZhi = CalcCore.CONST_DIZHI[
            (hourNo + 10) % 12 - 1
        ]  # 索引从0开始，需要减1

    # 2. 地空
    # 亥宫起    1 <=>11   2---10  3---9
    diKongDiZhi = CalcCore.CONST_DIZHI[(12 - hourNo) % 12 - 1]  # 索引从0开始，需要减1

    return [diJieDiZhi, diKongDiZhi]


@staticmethod
def Get_HuoXingLinXing_AtDiZhi(yearDiZhi: str, hourDiZhi: str) -> list:
    rDict = {"申子辰": "寅,戌", "寅午戌": "丑,卯", "巳酉丑": "卯,戌", "亥卯未": "酉,戌"}

    # 查找对应的地支组合
    dDit = None
    for key, value in rDict.items():
        if yearDiZhi in key:
            dDit = value
            break

    if not dDit:
        return ["", ""]

    hNo = CalcCore.DiZhi_No_MAP[hourDiZhi]
    huoXingDiZhi = ""
    lingXingDiZhi = ""

    if "寅" in dDit:
        # 1-->2    2-->3
        # 1-->10   2-->11
        if hNo > 10:
            huoXingDiZhi = CalcCore.CONST_DIZHI[(hNo + 1) % 12 - 1]
        else:
            huoXingDiZhi = CalcCore.CONST_DIZHI[hNo + 1 - 1]  # 索引从0开始，需要减1

        dizIdx = (hNo + 9) % 12 if hNo > 2 else hNo + 9
        lingXingDiZhi = CalcCore.CONST_DIZHI[dizIdx - 1]  # 索引从0开始，需要减1

    elif "丑" in dDit:
        # 1--> 1   2-->2
        # 1--> 3   2-->4
        dizIdx = hNo % 12
        huoXingDiZhi = CalcCore.CONST_DIZHI[dizIdx - 1]  # 索引从0开始，需要减1

        dizIdx = (hNo + 2) % 12 if hNo > 9 else hNo + 2
        lingXingDiZhi = CalcCore.CONST_DIZHI[dizIdx - 1]  # 索引从0开始，需要减1

    elif "卯," in dDit:
        # 1--> 3   2-->4
        # 1--> 10   2-->11
        dizIdx = (hNo + 2) % 12 if hNo > 9 else hNo + 2
        huoXingDiZhi = CalcCore.CONST_DIZHI[dizIdx - 1]  # 索引从0开始，需要减1

        dizIdx = (hNo + 9) % 12 if hNo > 2 else hNo + 9
        lingXingDiZhi = CalcCore.CONST_DIZHI[dizIdx - 1]  # 索引从0开始，需要减1

    elif "酉" in dDit:
        # 1--> 9    2-->10
        # 1--> 10   2-->11
        dizIdx = (hNo + 8) % 12 if hNo > 3 else hNo + 8
        huoXingDiZhi = CalcCore.CONST_DIZHI[dizIdx - 1]  # 索引从0开始，需要减1

        dizIdx = (hNo + 9) % 12 if hNo > 2 else hNo + 9
        lingXingDiZhi = CalcCore.CONST_DIZHI[dizIdx - 1]  # 索引从0开始，需要减1

    return [huoXingDiZhi, lingXingDiZhi]


@staticmethod
def Get_TianKuiTianYue(yearTianGan: str) -> list:
    rDict = {
        "甲戊庚": "丑,未",
        "乙己": "子,申",
        "辛": "午,寅",
        "壬癸": "卯,巳",
        "丙丁": "亥,酉",
    }

    for key, value in rDict.items():
        if yearTianGan in key:
            sArr = value.split(",")
            return [sArr[0], sArr[1]]

    return ["", ""]


@staticmethod
def Get_TianMaXing_DiZhi(diZhi: str, type_str: str = "year", month: str = "") -> str:
    if type_str == "year":
        doctM = {"寅午戌": "申", "申子辰": "寅", "巳酉丑": "亥", "亥卯未": "巳"}
        for key, value in doctM.items():
            if diZhi in key:
                return value
        return ""
    elif type_str == "yue" and month:
        doctM = {
            "正月一月五月九月": "申",
            "二月六月十月": "巳",
            "三月七月": "寅",
            "十一月冬月": "寅",
            "四月八月": "亥",
            "十二月腊月": "亥",
        }
        for key, value in doctM.items():
            if month in key:
                return value
        return ""
    else:
        # 精确版，使用节气定月份
        doctM = {"寅午戌": "申", "申子辰": "寅", "巳酉丑": "亥", "亥卯未": "巳"}
        for key, value in doctM.items():
            if diZhi in key:
                return value
        return ""


@staticmethod
def Get_LuCunAtDiZhi(yearTianGan: str) -> str:
    rDict = {
        "甲": "寅",
        "乙": "卯",
        "丙戊": "巳",
        "丁己": "午",
        "庚": "申",
        "辛": "酉",
        "壬": "亥",
        "癸": "子",
    }

    for key, value in rDict.items():
        if yearTianGan in key:
            return value

    return ""


@staticmethod
def Get_QingYang_AtDiZhi(luCunDiZhi: str) -> str:
    return CalcCore.DiZhiMoveNext(luCunDiZhi)


@staticmethod
def Get_TuoLu_AtDiZhi(luCunDiZhi: str) -> str:
    return CalcCore.DiZhiMoveBefore(luCunDiZhi)


# 其他副星 ...


@staticmethod
def Get_TianXingTianYaoAtDiZhi(monthNo: int) -> list:
    # 1. 先计算天刑 酉宫起 1<==>9 index
    # 2. 计算天姚   丑宫起 1<==>1 index
    if monthNo + 8 > 11:
        tianXing = CalcCore.CONST_DIZHI[(monthNo + 8) % 12]
    else:
        tianXing = CalcCore.CONST_DIZHI[monthNo + 8]

    if monthNo > 11:
        tianYao = CalcCore.CONST_DIZHI[monthNo % 12]
    else:
        tianYao = CalcCore.CONST_DIZHI[monthNo]

    return [tianXing, tianYao]


@staticmethod
def Get_HongLuanTianXiAtDiZhi(yearDiZhi: str) -> list:
    diZhiNo = CalcCore.DiZhi_No_MAP[yearDiZhi]  # 地支序号
    # 1--3   2---2,   3---1  4---0   5 --> 11   6 -->10
    if diZhiNo > 4:
        hongLuan = CalcCore.CONST_DIZHI[16 - diZhiNo - 1]  # 索引从0开始，需要减1
    else:
        hongLuan = CalcCore.CONST_DIZHI[4 - diZhiNo - 1]  # 索引从0开始，需要减1

    # 1-->9   2--->8   3-->7 4---6  9-->1    10 -->0     11-->11   12 --->10
    if diZhiNo > 10:
        tianXi = CalcCore.CONST_DIZHI[22 - diZhiNo - 1]  # 索引从0开始，需要减1
    else:
        tianXi = CalcCore.CONST_DIZHI[10 - diZhiNo - 1]  # 索引从0开始，需要减1

    return [hongLuan, tianXi]


@staticmethod
def Get_SanTaiBaZuo_AtDiZhi(
    zuoFuXingDiZhi: str, youBiXingDizhi: str, shengRiDiNo: int
) -> list:
    for i in range(shengRiDiNo):
        if i == 0:
            continue
        zuoFuXingDiZhi = CalcCore.DiZhiMoveNext(zuoFuXingDiZhi)

    for i in range(shengRiDiNo):
        if i == 0:
            continue
        youBiXingDizhi = CalcCore.DiZhiMoveBefore(youBiXingDizhi)

    return [zuoFuXingDiZhi, youBiXingDizhi]


@staticmethod
def Get_EnGuangTianGui_AtDiZhi(
    wenChangDiZhi: str, wenQuDizhi: str, shengRiDiNo: int
) -> list:
    for i in range(shengRiDiNo):
        if i == 0:
            continue
        wenChangDiZhi = CalcCore.DiZhiMoveNext(wenChangDiZhi)

    for i in range(shengRiDiNo):
        if i == 0:
            continue
        wenQuDizhi = CalcCore.DiZhiMoveNext(wenQuDizhi)

    wenChangDiZhi = CalcCore.DiZhiMoveBefore(wenChangDiZhi)
    wenQuDizhi = CalcCore.DiZhiMoveBefore(wenQuDizhi)

    return [wenChangDiZhi, wenQuDizhi]


@staticmethod
def Get_GuCenGuaShu_AtDiZhi(yearDiZhi: str) -> list:
    doctM = {"寅卯辰": "巳丑", "巳午未": "申辰", "申酉戌": "亥未", "亥子丑": "寅戌"}

    for key, value in doctM.items():
        if yearDiZhi in key:
            return [value[0], value[1]]

    return ["", ""]


@staticmethod
def Get_TianKuTianXu_AtDiZhi(yearDiZhi: str) -> list:
    yearNo = CalcCore.DiZhi_No_MAP[yearDiZhi]

    # tianxu  1-->6   2-->7  6-->11
    if yearNo > 6:
        tianXu = CalcCore.CONST_DIZHI[(yearNo + 5) % 12 - 1]  # 索引从0开始，需要减1
    else:
        tianXu = CalcCore.CONST_DIZHI[yearNo + 5 - 1]  # 索引从0开始，需要减1

    # tianku
    # 1--->6  2--5  7-->0   8-->11  9-->10
    if yearNo > 7:
        tianKu = CalcCore.CONST_DIZHI[19 - yearNo - 1]  # 索引从0开始，需要减1
    else:
        tianKu = CalcCore.CONST_DIZHI[7 - yearNo - 1]  # 索引从0开始，需要减1

    return [tianKu, tianXu]


@staticmethod
def Get_TianCaiTianShou_AtDiZhi(
    mingGongDiZhi: str, shenGongDiZhi: str, yearDiZhi: str
) -> list:
    yearIdxNo = CalcCore.DiZhi_No_MAP[yearDiZhi]
    for i in range(yearIdxNo):
        if i == 0:
            continue
        mingGongDiZhi = CalcCore.DiZhiMoveNext(mingGongDiZhi)
        shenGongDiZhi = CalcCore.DiZhiMoveNext(shenGongDiZhi)

    return [mingGongDiZhi, shenGongDiZhi]


@staticmethod
def Get_TaiFuFengGao_AtDiZhi(hourDiZhi: str) -> list:
    houIdxNo = CalcCore.DiZhi_No_MAP[hourDiZhi]

    # 1 6  2  7   6-->11  7 12
    tafu = CalcCore.CONST_DIZHI[(houIdxNo + 5) % 12 - 1]  # 索引从0开始，需要减1

    # 1-->2   2-->3  3-->0  4-->11  5-->10
    fanGao = CalcCore.CONST_DIZHI[(houIdxNo + 1) % 12 - 1]  # 索引从0开始，需要减1

    return [tafu, fanGao]


@staticmethod
def Get_TianGuanTianFu_AtDiZhi(yearGan: str) -> list:
    doctM = {
        "甲": "未酉",
        "乙": "辰申",
        "丙": "巳子",
        "丁": "寅亥",
        "戊": "卯卯",
        "己": "酉寅",
        "庚": "亥午",
        "辛": "酉巳",
        "壬": "戌午",
        "癸": "午巳",
    }

    for key, value in doctM.items():
        if yearGan in key:
            return [value[0], value[1]]

    return ["", ""]


@staticmethod
def Get_LongCiFengGe_AtDiZhi(yearDiZhi: str) -> list:
    yearIdxNo = CalcCore.DiZhi_No_MAP[yearDiZhi]

    # longci   1-->4  2-->5  8-->11  9 -->12
    longCi = CalcCore.CONST_DIZHI[(yearIdxNo + 3) % 12 - 1]  # 索引从0开始，需要减1

    # fengge   1-->10  2-->9  11-->0 12-->11
    fengGe = CalcCore.CONST_DIZHI[
        11 - yearIdxNo - 1 if yearIdxNo <= 11 else 11
    ]  # 索引从0开始，需要减1

    return [longCi, fengGe]


@staticmethod
def Get_JieShenAtDiZhi(month: str) -> str:
    doctM = {
        "申": "正月一月",
        "戌": "三月四月",
        "子": "五月六月",
        "寅": "七月八月",
        "辰": "九月十月",
        "午": "十一月十二月腊月",
    }

    for key, value in doctM.items():
        if month in value:
            return key

    return ""


@staticmethod
def Get_TianWuAtDiZhi(month: str) -> str:
    doctM = {
        "正月一月五月九月": "巳",
        "二月六月十月": "申",
        "三月七月十一月": "寅",
        "四月八月十二月腊月": "亥",
    }

    for key, value in doctM.items():
        if month in key:
            return value

    return ""


@staticmethod
def Get_YinShaAtDiZhi(month: str) -> str:
    doctM = {
        "正月一月七月": "寅",
        "二月八月": "子",
        "三月九月": "戌",
        "四月十月": "申",
        "五月十一月": "午",
        "六月十二月腊月": "辰",
    }

    for key, value in doctM.items():
        if month in key:
            return value

    return ""


@staticmethod
def Get_TianYue_AtDiZhi(month: str) -> str:
    doctM = {
        "正月": "戌",
        "一月": "戌",
        "二月": "巳",
        "三月": "辰",
        "四月": "寅",
        "五月": "未",
        "六月": "卯",
        "七月": "亥",
        "八月": "未",
        "九月": "寅",
        "十月": "午",
        "十一月": "戌",
        "冬月": "戌",
        "十二月": "寅",
        "腊月": "寅",
    }

    for key, value in doctM.items():
        if month in key:
            return value

    return ""


@staticmethod
def Get_TianKong_AtDiZhi(yearDiZhi: str) -> str:
    dNo = CalcCore.DiZhi_No_MAP[yearDiZhi]

    if dNo == 12:
        diZHi = "丑"
    else:
        diZHi = CalcCore.CONST_DIZHI[dNo - 1]  # 索引从0开始，需要减1

    return diZHi


@staticmethod
def Get_JieKong_AtDiZhi(yearGan: str) -> str:
    doctM = {
        "甲己": "申酉",
        "乙庚": "午未",
        "丙辛": "辰巳",
        "丁壬": "寅卯",
        "戊癸": "子丑",
    }

    for key, value in doctM.items():
        if yearGan in key:
            idx = key.index(yearGan)
            return value[idx]

    return ""


@staticmethod
def Get_PoSui_AtDiZhi(yearDiZhi: str) -> str:
    doctM = {"子午卯酉": "巳", "寅申巳亥": "酉", "辰戌丑未": "丑"}

    for key, value in doctM.items():
        if yearDiZhi in key:
            return value

    return ""


@staticmethod
def Get_FeiLian_AtDiZhi(yearDiZhi: str) -> str:
    doctM = {
        "子丑寅": "申酉戌",
        "卯辰巳": "巳午未",
        "午未申": "寅卯辰",
        "酉戌亥": "亥子丑",
    }

    for key, value in doctM.items():
        if yearDiZhi in key:
            idx = key.index(yearDiZhi)
            return value[idx]

    return ""


@staticmethod
def Get_TianChu_AtDiZhi(yearGan: str) -> str:
    doctM = {
        "甲丁": "巳",
        "乙戊辛": "午",
        "丙": "子",
        "己": "申",
        "庚": "寅",
        "壬": "酉",
        "癸": "亥",
    }

    for key, value in doctM.items():
        if yearGan in key:
            return value

    return ""


@staticmethod
def Get_TianSangTianShi_AtDiZhi(jiaoYouGongDiZhi: str, jiEGongDiZhi: str) -> list:
    # 天伤在交友宫，天使在疾厄宫。
    return [jiaoYouGongDiZhi, jiEGongDiZhi]


@staticmethod
def Get_YueDe_AtDiZhi(yearDiZhi: str) -> str:
    dDoct = {
        "子": "巳",
        "丑": "午",
        "寅": "未",
        "卯": "申",
        "亥": "辰",
        "辰": "酉",
        "戌": "卯",
        "巳": "戌",
        "酉": "寅",
        "申": "丑",
        "未": "子",
        "午": "亥",
    }

    return dDoct.get(yearDiZhi, "")


# -----------------杂星-----------
@staticmethod
def Get_ChangShengShiErShen(wuXingJu: str, sexYingYang, yearGan: str) -> dict:
    shierStar = [
        "长生",
        "沐浴",
        "冠带",
        "临官",
        "帝旺",
        "衰",
        "病",
        "死",
        "墓",
        "绝",
        "胎",
        "养",
    ]
    dDoct = {
        "水二局": "申",
        "木三局": "亥",
        "金四局": "巳",
        "土五局": "申",
        "火六局": "寅",
    }

    # 查找对应的五行局起始地支
    start_dizhi = dDoct.get(wuXingJu, "")
    if not start_dizhi:
        return {}

    wuxingJuIdxNo = CalcCore.DiZhi_No_MAP[start_dizhi]  # 开始起点

    # 查找天干索引
    gIdx = 1
    for i, item in enumerate(CalcCore.CONST_TIANGAN):
        if yearGan == item:
            gIdx = i + 1  # 转换为1-based索引
            break

    axis = 1  # >0 表示顺行  <0 表示逆行

    isEven = gIdx % 2 == 0  # 偶数
    if isEven and sexYingYang == CalcCore.YinYangEnum.阳男:
        # 阴男
        axis = axis * -1
    elif not isEven and sexYingYang == CalcCore.YinYangEnum.阳男:
        # 阳男
        axis = axis * 1
    elif not isEven and sexYingYang == CalcCore.YinYangEnum.阴女:
        # 阳女
        axis = axis * -1
    elif isEven and sexYingYang == CalcCore.YinYangEnum.阴女:
        # 阴女
        axis = axis * 1

    rtnDict = {}
    tmpZiZhi = start_dizhi
    for i, xingItem in enumerate(shierStar):
        if i == 0:
            rtnDict[tmpZiZhi] = xingItem
            continue

        if axis > 0:
            tmpZiZhi = CalcCore.DiZhiMoveNext(tmpZiZhi)
            rtnDict[tmpZiZhi] = xingItem
        else:
            tmpZiZhi = CalcCore.DiZhiMoveBefore(tmpZiZhi)
            rtnDict[tmpZiZhi] = xingItem

    return rtnDict


@staticmethod
def Get_BoShiShiErShenXing(luCunXingAtDiZhi: str, sexYingYang, yearGan: str) -> dict:
    # Comment 病符特殊标识 加了空格，小程序 same set key be warn (191209)
    shierStar = [
        "博士",
        "力士",
        "青龙",
        "小耗",
        "将军",
        "奏书",
        "飞廉",
        "喜神",
        "病符 ",
        "大耗",
        "伏兵",
        "官府",
    ]

    # 查找天干索引
    gIdx = 1
    for i, item in enumerate(CalcCore.CONST_TIANGAN):
        if yearGan == item:
            gIdx = i + 1  # 转换为1-based索引
            break

    axis = 1  # >0 表示顺行  <0 表示逆行

    isEven = gIdx % 2 == 0  # 偶数
    if isEven and sexYingYang == CalcCore.YinYangEnum.阳男:
        # 阴男
        axis = axis * -1
    elif not isEven and sexYingYang == CalcCore.YinYangEnum.阳男:
        # 阳男
        axis = axis * 1
    elif not isEven and sexYingYang == CalcCore.YinYangEnum.阴女:
        # 阳女
        axis = axis * -1
    elif isEven and sexYingYang == CalcCore.YinYangEnum.阴女:
        # 阴女
        axis = axis * 1

    rtnDict = {}
    tmpZiZhi = luCunXingAtDiZhi
    for i, xingItem in enumerate(shierStar):
        if i == 0:
            rtnDict[tmpZiZhi] = xingItem
            continue

        if axis > 0:
            tmpZiZhi = CalcCore.DiZhiMoveNext(tmpZiZhi)
            rtnDict[tmpZiZhi] = xingItem
        else:
            tmpZiZhi = CalcCore.DiZhiMoveBefore(tmpZiZhi)
            rtnDict[tmpZiZhi] = xingItem

    return rtnDict


@staticmethod
def Get_SuiQianShiErShenXing(yearDiZhi: str) -> dict:
    # 即按年支起太岁，不分阴阳顺逆，一律安十二星
    shierStar = [
        "岁建",
        "晦气",
        "丧门",
        "贯索",
        "官府",
        "小耗",
        "大耗",
        "龙德",
        "白虎",
        "天德",
        "吊客",
        "病符",
    ]

    rtnDict = {}
    tmpZiZhi = yearDiZhi
    for i, xingItem in enumerate(shierStar):
        if i == 0:
            rtnDict[tmpZiZhi] = xingItem
        else:
            tmpZiZhi = CalcCore.DiZhiMoveNext(tmpZiZhi)
            rtnDict[tmpZiZhi] = xingItem

    return rtnDict


@staticmethod
def Get_LiuQianShiErShenXing(yearDiZhi: str) -> dict:

    shierStar = [
        "将星",
        "攀安",
        "岁驿",
        "息神",
        "华盖",
        "劫煞",
        "灾煞",
        "天煞",
        "指背",
        "咸池",
        "月煞",
        "亡神",
    ]

    dDoct = {"申子辰": "子", "寅午戌": "午", "亥卯未": "卯", "巳酉丑": "酉"}

    # 查找对应的起始地支
    start_dizhi = ""
    for key, value in dDoct.items():
        if yearDiZhi in key:
            start_dizhi = value
            break

    if not start_dizhi:
        return {}

    rtnDict = {}
    tmpZiZhi = start_dizhi
    for i, xingItem in enumerate(shierStar):
        if i == 0:
            rtnDict[tmpZiZhi] = xingItem
        else:
            tmpZiZhi = CalcCore.DiZhiMoveNext(tmpZiZhi)
            rtnDict[tmpZiZhi] = xingItem

    return rtnDict


@staticmethod
def Get_HuaGai_AtDiZhi(yearDiZhi: str) -> str:
    doctM = {"子辰申": "辰", "丑巳酉": "丑", "寅午戌": "戌", "卯未亥": "未"}

    for key, value in doctM.items():
        if yearDiZhi in key:
            return value

    return ""


@staticmethod
def GetStarLightLevel(xingName: str, dizhi: str) -> str:
    if xingName in CommonVarRule.Xing_light_level:
        try:
            idx = CommonVarRule.CONST_DIZHI.index(dizhi)
            return CommonVarRule.Xing_light_level[xingName][idx]
        except ValueError:
            # 如果地支不在CONST_DIZHI中，返回空字符串
            return ""
    return ""


@staticmethod
def GetLaiYinGong(mingZao, shierGong):
    yNianGan = mingZao.yearTianGan

    fd = None
    for gong in shierGong:
        if gong.GongTianGan == yNianGan and (
            gong.GongDiZhi != "子" or gong.GongDiZhi != "丑"
        ):
            fd = gong
            break

    if fd is not None:
        # 设置来因宫标志
        fd.IsLaiYinGong = True
        pass  # 根据注释，原C#代码中这部分被注释掉了
